package com.zgkj.Zip.tools;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.pdf.PdfContentByte;
import com.itextpdf.text.pdf.PdfCopy;
import com.itextpdf.text.pdf.PdfImportedPage;
import com.itextpdf.text.pdf.PdfReader;
import com.itextpdf.text.pdf.PdfWriter;
import com.itextpdf.text.pdf.RandomAccessFileOrArray;
import com.itextpdf.text.pdf.codec.TiffImage;

public class TifToPDFUtil {
	//将多个tif文件合并成一个pdf文件
	public static void tifToPdf(String[] arr, String sFilePdf) {
		String sNewfile = sFilePdf.substring(0, sFilePdf.lastIndexOf("\\"));
		File _toFile = new File(sNewfile);
		if (!_toFile.exists()) {//如果pdf目录不存在新建目录
			_toFile.mkdirs();
		}
		// 创建一个文档对象
//		Document doc = new Document(PageSize.LETTER, 0, 0, 0, 0);
		Document doc = new Document(PageSize.A4, 20, 20, 20, 20);
		int pages = 0;
		int comps = 0;
		String sFileTif = "";
		try {
			// 定义输出位置并把文档对象装入输出对象中
			PdfWriter writer = PdfWriter.getInstance(doc, new FileOutputStream(
					sFilePdf));
			// 打开文档对象
			doc.open();
			for (String element : arr) {
				sFileTif = element;
				PdfContentByte cb = writer.getDirectContent();
				RandomAccessFileOrArray ra = null;
				try {
					ra = new RandomAccessFileOrArray(sFileTif);
					comps = TiffImage.getNumberOfPages(ra);
				} catch (Throwable e) {
					System.out.println("Exception in " + sFileTif + " "
							+ e.getMessage());
				}
				// System.out.println("Processing: " + sFileTif);
				for (int c = 0; c < comps; ++c) {
					try {
						com.itextpdf.text.Image img = TiffImage.getTiffImage(
								ra, c + 1);
						if (img != null) {
							// System.out.println("page " + (c + 1));
//							System.out.println(img.getDpiX());
//							System.out.println(img.getDpiY());
							img.scalePercent(7200f / img.getDpiX(), 7200f / img
									.getDpiY());
//							System.out.println(img.getScaledWidth());
//							System.out.println(img.getScaledHeight());
							doc.setPageSize(new Rectangle(img.getScaledWidth(),
									img.getScaledHeight()));
							img.setAbsolutePosition(0, 0);
							cb.addImage(img);
							doc.newPage();
							++pages;
						}
					} catch (Throwable e) {
						System.out.println("Exception " + sFileTif + " page "
								+ (c + 1) + " " + e.getMessage());
					}
				}
				ra.close();// 关闭
			}
			// 关闭文档对象，释放资源
			doc.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static boolean mergePdfFiles(String[] files, String newfile) {
		boolean retValue = false;
		Document document = null;
		try {
			document = new Document(new PdfReader(files[0]).getPageSize(1));
			PdfCopy copy = new PdfCopy(document, new FileOutputStream(newfile));
			document.open();

			for (String file : files) {

				PdfReader reader = new PdfReader(file);
				int n = reader.getNumberOfPages();
				for (int j = 1; j <= n; j++) {
					document.newPage();
					PdfImportedPage page = copy.getImportedPage(reader, j);
					copy.addPage(page);
				}
			}
			retValue = true;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			document.close();
		}
		return retValue;
	}


	public static void main(String[] args) {
		String[] arr = new String[8];
		arr[0]= "D:\\cpc\\缴费通知书2017030100381720\\GA000133086583\\GA000133086583\\000001.tif";
		arr[1]= "D:\\cpc\\缴费通知书2017030100381720\\GA000133086583\\GA000133086583\\000002.tif";
		arr[2]= "D:\\cpc\\缴费通知书2017030100381720\\GA000133086583\\GA000133086583\\000003.tif";
		arr[3]= "D:\\cpc\\缴费通知书2017030100381720\\GA000133086583\\GA000133086583\\000004.tif";
		arr[4]= "D:\\cpc\\缴费通知书2017030100381720\\GA000133086583\\GA000133086583\\000005.tif";
		arr[5]= "D:\\cpc\\缴费通知书2017030100381720\\GA000133086583\\GA000133086583\\000006.tif";
		arr[6]= "D:\\cpc\\缴费通知书2017030100381720\\GA000133086583\\GA000133086583\\000007.tif";
		arr[7]= "D:\\cpc\\缴费通知书2017030100381720\\GA000133086583\\GA000133086583\\000008.tif";

		TifToPDFUtil.tifToPdf(arr, "D:\\cpc\\缴费通知书2017030100381720\\GA000133086583\\GA000133086583\\123.pdf");
	}
}
